﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace DevExpress.MailClient.Win.Forms
{
    public partial class frmAddMailAttachments : PRO_DOCS.BLL.WIN.Templates.DevxPermissionTemplate
    {
        public Dictionary<Guid, PRO_DOCS.DAL.Database.INTERNAL_MAIL_ATTACHMENT> MAIL_ATTACHMENT_NAMES
        {
            get;
            set;
        }

        List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHE> APPLICATION_SAVING_PATHES
        { get; set; }

        Data.Message SOURCE_MESSAGE
        { get; set; }

        public frmAddMailAttachments(bool iS_NEW_EMAIL, Data.Message sOURCE_MESSAGE)
        {
            InitializeComponent();


            APPLICATION_SAVING_PATHES = PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHEs.ToList();
            
            SOURCE_MESSAGE = sOURCE_MESSAGE;

            tsAttachmentsControl.Visible = iS_NEW_EMAIL;

            this.Text = (iS_NEW_EMAIL) ? this.Text : "Preview Email Attachments";

            MAIL_ATTACHMENT_NAMES = SOURCE_MESSAGE.INTERNAL_MAIL_ATTACHMENTS.ToDictionary(k => k.INTERNAL_MAIL_ATTACHMENT_ID, v => v);

            List<DevExpress.MailClient.Win.Data.UploadedPhysicalFiles> selectedFilePathes = new List<DevExpress.MailClient.Win.Data.UploadedPhysicalFiles>();

            foreach (var mAIL_ATTACHMENT in MAIL_ATTACHMENT_NAMES)
            {

                PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY dEPARTMENT_FILE_HISTORY = (from history in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENT_FILE_HISTORies
                                                                                                 where history.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID.Equals(mAIL_ATTACHMENT.Value.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID.Value)
                                                                                                 select history).FirstOrDefault();

                string fullDownloadReportPath = string.Empty;
                FileInfo selectedFileInfo = null;
                for (int i = 0; i < APPLICATION_SAVING_PATHES.Count; i++)
                {
                    fullDownloadReportPath = Path.Combine(APPLICATION_SAVING_PATHES[i].ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH, mAIL_ATTACHMENT.Value.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID.Value.ToString());

                    selectedFileInfo = new FileInfo(fullDownloadReportPath);

                    if (selectedFileInfo.Exists)
                    {
                        DevExpress.MailClient.Win.Data.UploadedPhysicalFiles single_uploaded_physical_file = new DevExpress.MailClient.Win.Data.UploadedPhysicalFiles();
                        single_uploaded_physical_file.FileId = dEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID;
                        single_uploaded_physical_file.FileInfo = selectedFileInfo;
                        single_uploaded_physical_file.FileDisplayName = dEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_DISPLAY_NAME;
                        single_uploaded_physical_file.Extension = dEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_EXTENSION;
                        selectedFilePathes.Add(single_uploaded_physical_file);
                        break;
                    }

                }
            }
            gridControl1.DataSource = selectedFilePathes;
        }

        private void tsbAddStepAttachmnets_Click(object sender, EventArgs e)
        {
            List<DevExpress.MailClient.Win.Data.UploadedPhysicalFiles> selectedFilePathes = new List<DevExpress.MailClient.Win.Data.UploadedPhysicalFiles>();

            OpenFileDialog openFileDialogObj = new OpenFileDialog();
            openFileDialogObj.Title = "Choose Department Files.";
            openFileDialogObj.Multiselect = true;
            openFileDialogObj.FilterIndex = 1;
            openFileDialogObj.RestoreDirectory = true;
            if (openFileDialogObj.ShowDialog() == DialogResult.OK)
            {
                gridControl1.DataSource = null;


                string[] selectedFiles = openFileDialogObj.FileNames;

                for (int i = 0; i < selectedFiles.Length; i++)
                {
                    string fullDownloadReportPath = selectedFiles[i];
                    FileInfo selectedFileInfo = new FileInfo(fullDownloadReportPath);

                    DevExpress.MailClient.Win.Data.UploadedPhysicalFiles single_uploaded_physical_file = new DevExpress.MailClient.Win.Data.UploadedPhysicalFiles();
                    single_uploaded_physical_file.ArchiveFileId = Guid.Empty;
                    single_uploaded_physical_file.CheckedFile = true;
                    single_uploaded_physical_file.FileId = Guid.NewGuid();
                    single_uploaded_physical_file.FileInfo = selectedFileInfo;
                    single_uploaded_physical_file.FileDisplayName = selectedFileInfo.Name;
                    single_uploaded_physical_file.UploadingMessage = string.Empty;
                    single_uploaded_physical_file.CurrentIcon = "none";
                    single_uploaded_physical_file.Extension = selectedFileInfo.Extension;
                    selectedFilePathes.Add(single_uploaded_physical_file);
                }

                gridControl1.DataSource = selectedFilePathes;
            }
        }

        private void tsbDeleteFile_Click(object sender, EventArgs e)
        {
            DevExpress.MailClient.Win.Data.UploadedPhysicalFiles single_uploaded_physical_file = gridView1.GetFocusedRow() as DevExpress.MailClient.Win.Data.UploadedPhysicalFiles;

            MAIL_ATTACHMENT_NAMES.Remove(single_uploaded_physical_file.FileId);

            gridView1.DeleteSelectedRows();
        }

        private void tsbSaveMailAttachments_Click(object sender, EventArgs e)
        {
            MAIL_ATTACHMENT_NAMES = new Dictionary<Guid, PRO_DOCS.DAL.Database.INTERNAL_MAIL_ATTACHMENT>();

            for (int i = 0; i < gridView1.DataRowCount; i++)
            {
                DevExpress.MailClient.Win.Data.UploadedPhysicalFiles single_uploaded_physical_file = gridView1.GetRow(i) as DevExpress.MailClient.Win.Data.UploadedPhysicalFiles;

                //MAIL_ATTACHMENT_NAMES.
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle > -1)
            {
                DevExpress.MailClient.Win.Data.UploadedPhysicalFiles single_uploaded_physical_file = gridView1.GetFocusedRow() as DevExpress.MailClient.Win.Data.UploadedPhysicalFiles;

                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Title = "Select Saving Path";
                sfd.AddExtension = true;
                sfd.DefaultExt = single_uploaded_physical_file.Extension.Replace(".", string.Empty);
                sfd.FileName = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, grdFilePath).ToString();

                if (sfd.ShowDialog().Equals(DialogResult.OK))
                {
                    single_uploaded_physical_file.FileInfo.CopyTo(sfd.FileName, true);
                }
            }
        }
    }
}